package com.xdtech.game.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.game.service.RankService;
import com.xdtech.game.vo.RankItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2015-03-16 23:17:03
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class RankController {
	@Autowired
	private RankService rankService;
	@RequestMapping(value="/rank.do",params = "rank")
	public ModelAndView skipRank() {
		return new ModelAndView("game/rank/rank_ftl");
	}
	
	@RequestMapping(value="/rank.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处�?
			results = rankService.loadPageAndCondition(null, null);
		}else {
			results = rankService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(value="/rank.do",params = "editRank")
	public ModelAndView editRank(HttpServletRequest request,Long rankId) {
		if (rankId!=null) {
			request.setAttribute("rankItem", rankService.loadRankItem(rankId));
		}
		return new ModelAndView("game/rank/editRank_ftl");
	}
	
	@RequestMapping(value={"/rank.do","/rank.action"},params = "saveRank")
	@ResponseBody
	public ResultMessage saveRank(RankItem item) {
		ResultMessage r = new ResultMessage();
		//rank.action?saveRank&score=99&uname=zhangsan&uid=123&gameCode=dfj
		if (rankService.saveOrUpdateRank(item)) {
			//根据id获取排名
			int paiming = rankService.getSort(item);
			r.setObj(paiming);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/rank.do",params = "deleteRankItems")
	@ResponseBody
	public ResultMessage deleteRankItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> rankIds = new ArrayList<Long>();
			for (String id : tempIds) {
				rankIds.add(Long.valueOf(id));
			}
			rankService.deleteRankInfo(rankIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/rank.action",params="loadTop")
	@ResponseBody
	public Map<String, Object> loadTop(HttpServletRequest request,String gameCode,int topNum) {
		Map<String, Object> results = null;
		results = rankService.loadTopRank(gameCode,topNum);
		return results;
	}

}
